package yz.cp.back.auth.security;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import yz.cp.back.auth.ImConfig;

import java.util.List;

public class ImAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    private AuthenticationSuccessHandler authenticationSuccessHandler;

    private AuthenticationFailureHandler authenticationFailureHandler;

    private UserDetailsService userDetailsService;
    private ImConfig imConfig;


    @Override
    public void configure(HttpSecurity http) throws Exception {
        List<AntPathRequestMatcher> list= Lists.newArrayList();
        Table<String,String,String> authTable= HashBasedTable.create();
        for (String s:imConfig.getLoginUrl()){
            String[] arr=s.split("\\|");
            if(arr.length>0){
                list.add(new AntPathRequestMatcher(arr[0].trim(),"POST"));
            }
            if(arr.length>0 && arr.length==2){
                authTable.put(arr[0].trim(),arr[1].trim(),"json");
            }else {
                authTable.put(arr[0].trim(),arr[1].trim(),arr[2].trim());
            }
        }
        if(list.size()==0){
            list.add(new AntPathRequestMatcher("login","POST"));
        }

        ImAuthenticationFilter imAuthenticationFilter=new ImAuthenticationFilter(new OrRequestMatcher(list.stream().toArray(AntPathRequestMatcher[]::new)));
        imAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        imAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        imAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
        ImAuthenticationProvider imAuthenticationProvider=new ImAuthenticationProvider();
        imAuthenticationProvider.setUserDetailsService(userDetailsService);
        http.authenticationProvider(imAuthenticationProvider)
                .addFilterBefore(imAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

    public void setAuthenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
        this.authenticationFailureHandler = authenticationFailureHandler;
    }

    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler authenticationSuccessHandler) {
        this.authenticationSuccessHandler = authenticationSuccessHandler;
    }

    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }


    public void setImConfig(ImConfig imConfig) {
        this.imConfig = imConfig;
    }
}